/**
 *
 */
package com.darren.befriend.utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.darren.befriend.R;
import com.darren.befriend.views.MyCustomDialog;
import com.darren.befriend.views.MyCustomDialog.OnNegativeButtonClick;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @author Sniper
 * 
 */
public class Utilities {
	/**
	 * set fontTypeFace for edittext
	 * 
	 * @param edt
	 */
	public static void setFontType(Context context, View edt) {
		Typeface fontMyRiad = Typeface.createFromAsset(context.getAssets(),
				"HelveticaLTStd-Roman.otf");
		if (edt instanceof EditText) {

			((EditText) edt).setTypeface(fontMyRiad);
		} else if (edt instanceof TextView) {

			((TextView) edt).setTypeface(fontMyRiad);
		}
	}

	/**
	 * Get current user location
	 * 
	 * @param context
	 *            {@link Context}
	 * @return Current user location
	 * */
	public static Address getCurrentLocation(Context context) {

		try {

			LocationManager lm = (LocationManager) context
					.getSystemService(Context.LOCATION_SERVICE);
			Location location = lm
					.getLastKnownLocation(LocationManager.GPS_PROVIDER);
			LogUtil.e("getCurrentLocation: ", LocationManager.GPS_PROVIDER);
			if (location == null) {

				LogUtil.e("getCurrentLocation: ",
						LocationManager.NETWORK_PROVIDER);
				location = lm
						.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
			}

			if (location != null) {

				Geocoder geoCode = new Geocoder(context, Locale.getDefault());
				try {
					List<Address> addresses = geoCode.getFromLocation(
							location.getLatitude(), location.getLongitude(), 1);
					if (addresses.size() > 0) {

						return addresses.get(0);
					}
				} catch (IOException e) {

					e.printStackTrace();
				}
			}

		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;
	}

	/**
	 * get Screen height in pixel
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenHeight(Context context) {

		DisplayMetrics displaymetrics = new DisplayMetrics();
		((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay().getMetrics(displaymetrics);
		return displaymetrics.heightPixels;
	}

	/**
	 * request hide keyboard
	 */
	public static void hideKeyboard(Activity activity) {

		InputMethodManager imm = (InputMethodManager) activity
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(activity.getWindow().getDecorView()
				.getApplicationWindowToken(), 0);
	}

	public static ArrayList<String> readFileAssest(Context context,
			String filename) {
		ArrayList<String> universityList = new ArrayList<String>();
		BufferedReader reader = null;
		try {

			reader = new BufferedReader(new InputStreamReader(context
					.getAssets().open("university.txt"), "UTF-8"));
			String line = reader.readLine();
			while (line != null) {

				int index = line.lastIndexOf(",");
				if (index != -1) {

					universityList.add(line.substring(index + 1));
				} else {

					universityList.add(line);
				}

				line = reader.readLine();
			}
		} catch (Exception e) {

			LogUtil.e("readFileAssest", e.getMessage());
		} finally {

			if (reader != null) {
				try {

					reader.close();
				} catch (IOException e) {

					e.printStackTrace();
				}
			}
		}

		return universityList;
	}

	/**
	 * show alert message
	 * 
	 * @param message
	 *            Alert message
	 * @param title
	 *            alert title
	 * */
	public static void showAlertMessage(Context context, String message,
			String title) {

		final MyCustomDialog dialog = new MyCustomDialog(context);
		dialog.setCancelableFlag(false);
		dialog.setTitle(title);
		dialog.setMessage(message);
		dialog.setNegativeButton(context.getString(R.string.ok),
				new OnNegativeButtonClick() {

					@Override
					public void onButtonClick(final View view) {
						// TODO Auto-generated method stub

						dialog.dismiss();
					}
				});

		dialog.show();
	}

	public static Bitmap rotateBitmap(Bitmap source, float angle) {

		if (source != null) {
			Matrix matrix = new Matrix();
			matrix.postRotate(angle);
			return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
					source.getHeight(), matrix, true);
		}

		return source;
	}

}
